# encoding=utf-8
import pickle

from scipy import interpolate

from application.logging import logger
from application.model.outside_temperature.connect_mysql_read_weather_data import connect_mysql_read_weather_data
from application.model.outside_temperature.dataprocess import dataprocesses
from application.model.outside_temperature.get_model_path_by_model_name import get_model_path_by_model_name
from application.model.outside_temperature.get_weather_timestamp_list import get_weather_timestamp_list


def get_weather_model(area_code=None):
    """
    生成天气模型
    :param area_code: 地区编码
    :return:
    """
    # 获取数据
    logger.info(f"获取数据:开始{'->' * 10}")
    data = connect_mysql_read_weather_data(area_code=area_code)
    # 打印数据
    logger.info(data)
    logger.info(f"获取数据:结束{'<-' * 10}")
    #
    try:
        # 时间列表:时间戳
        time_ = get_weather_timestamp_list(data=data)
        # 天气数据
        weather = dataprocesses(data)
        # 天气数据:打印
        logger.info(f"天气数据:{weather}")
        # X:时间维度,y:对应天气
        x = time_
        y = weather
        logger.info(f"x:{len(x)}-{x}\n y:{len(y)}-{y}")
        # 模型训练:差值模型
        f = interpolate.interp1d(x=x, y=y)
        print(f)
        # 模型名称
        model_name = f"cha_zhi_cubic_weather_{area_code}.pkl"
        logger.info(f"模型名称:{model_name}")
        # 模型路径
        model_path = get_model_path_by_model_name(model_name=model_name)
        logger.info(f"模型路径: {model_path}")
        # 存储模型
        pickle.dump(f, open(model_path, "wb"))
        # 存储成功
        logger.info(f"模型存储成功:{model_path}")
        return model_name
        pass
    except BaseException as err:
        logger.error(f"模型存储失败:{err}")
        pass
    pass


if __name__ == '__main__':
    get_weather_model(area_code="110106000000")
    pass
